<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        div {
            width: 1200px;
            height: 400px;
            margin: 50px auto;
            border: 1px solid red;
            overflow: hidden;
        }
        div li {
            width: 240px;
            height: 400px;
            float: left;
        }
        div ul {
            width: 1300px;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
     <script>
        //获取任意一个元素的任意一个属性的当前的值---当前属性的位置值
        function getStyle(element,attr){
            if(window.getComputedStyle){
                return window.getComputedStyle(element,null)[attr];
            }else{
                return element.currentStyle[attr];
            }
        }
        //getStyle(box,'top')
         

        function animation(element,json,fn){
            clearInterval(element.timer);
            element.timer=setInterval(function(){
                var flag = true;//假设全部到达目标
                for(var attr in json){
                    if(attr == "opacity"){
                        var current = getStyle(element,attr)*100;
                        var target = json[attr]*100;
                        var step = (target-current)/10;
                        step=step>0?Math.ceil(step):Math.floor(step);
                        current+=step;
                        element.style[attr] = current/100;
                    }else if(attr == "zIndex"){
                        element.style[attr] = json[attr];
                    }else{
                        var current = parseInt(getStyle(element,attr));
                        var target = json[attr];
                        var step = (target-current)/10;
                        step=step>0?Math.ceil(step):Math.floor(step);
                        current+=step;
                        element.style[attr] = current+'px';
                    }
                    if(current != target){
                        flag=false;
                    }
                }

                if (flag) {
                    //清理定时器
                    clearInterval(element.timer);
                    //所有的属性到达目标才能使用这个函数,前提是用户传入了这个函数
                    if (fn) {
                        fn();
                    }
                }
            },20)
        }

        var box = document.getElementById('box');
        var list = document.getElementsByTagName('li');
        for(var i=0;i<list.length;i++){
            list[i].style.backgroundImage="url(images/s"+(i+1)+".jpg)";
            list[i].onmouseover=function(){
                for(var j=0;j<list.length;j++){
                    animation(list[j],{"width":100})
                }
                animation(this,{"width":800}) 
            }
            list[i].onmouseout=function(){
                for(var j=0;j<list.length;j++){
                    animation(list[j],{"width":240})
                }
            }
        }
     
     
     
     
     </script>         
</body>
</html>